import { reactive, ref } from 'vue'
import { hasOwnProp } from '../utils/normalUtils'


const emailReg = /^[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+[\.a-zA-Z]+$/
export const validEmailFunc = (rule: any, value: any, callback: any) => {
    if (!value)callback()
    if (!emailReg.test(value)) callback(new Error('请输入正确格式！'))
    else callback()
}

// 规则放宽 只要是基本的数字和-组合
const mobileReg = /^\d{11}$|^(\d{1,4}-?|\(\d{1,4}\))?[1-9]\d{4,7}(-\d{1,8})?$/
export const validPhoneFunc = (rule: any, value: any, callback: any) => {
    if (!value)callback()
    if (!mobileReg.test(value)) callback(new Error('请输入正确格式！'))
    else callback()
}

// 规则放宽 手机11个数字就可以
const mobileReg2 = /^\d{11}$/
export const validMobileFunc = (rule: any, value: any, callback: any) => {
    if (!value)callback()
    if (!mobileReg2.test(value)) callback(new Error('请输入正确格式！'))
    else callback()
}

// ---------------------------------------------------------------------------------------------------------------------


/**
 * 重置表单
 */
export const resetDiyForm = (formError:any) => {
    for (const att in formError) {
        formError[att] = ''
    }
}

/**
 * 验证表单
 */
export const validDiyForm = (formData:any, formRules:any, formError:any) => {
    let flag = true
    for (const prop in formRules) {
        if (!validDiyFormItem(prop, formData, formRules, formError)) flag = false
    }
    return flag
}


/**
 *
 * @param prop
 * @param formData
 * @param formRules
 * @param formError
 */
export const validDiyFormItem = (prop:any, formData:any, formRules:any, formError:any) => {
    let flag = true
    if (!formRules || !hasOwnProp(formRules, prop)) return true
    const validArr = formRules[prop]
    for (const valid of validArr) {
        if (valid.required) {
            const value = String(formData[prop] || '').trim()
            const errorResult = value === ''
            formError[prop] = errorResult ? (valid.message || '不能为空') : ''
            if (errorResult) flag = false
        } else if (valid.validator) {
            const result = valid.validator(formData[prop], valid, formData)
            if (!result) {
                formError[prop] = valid.message
                flag = false
            }
        }
    }
    return flag
}

export function useDiyFormValid(options:any = null) {
    const formData = ref({})
    const formError:any = ref({})
    let formRules = {}
    if (options) {
        if (options.formData) {
            formData.value = options.formData
            for (const att in options.formData) {
                formError[att] = ''
            }
        }
        if (options.rules) formRules = options.rules
    }
    const actions = {
        getFormOptions: () => {
            return { formData: formData.value, rules: formRules, error: formError }
        },
        validForm: () => {
            actions.resetForm()
            const { formData, rules, error } = actions.getFormOptions()
            return validDiyForm(formData, rules, error)
        },
        validFormItem: (prop: string) => {
            const { formData, rules, error } = actions.getFormOptions()
            return validDiyFormItem(prop, formData, rules, error)
        },
        resetForm: () => {
            const { error } = actions.getFormOptions()
            resetDiyForm(error)
        }
    }

    return {
        formData,
        formError,
        actions
    }
}
